package dgx.software.com.ValidationPackage;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.*;

public class EMailValidationServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	// Call the doPost() Method
	doPost(request, response);
	
  }

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	// "RFC822" or "RFC2822"
	
	String EMail = request.getParameter("EMail");
	System.out.println(EMail);
	isValidEmailAddress(EMail);

}

private void isValidEmailAddress(String EMail) {

	// If we received an Empty E-Mail return an Error
	if(EMail.equals("")){
		throw new RuntimeException("No E-Mail provided. Please provide an E-Mail.");
	}
	
// This Java E-Mail Validation Regex was generated by converting the JavaScript E-Mail Validation Regex
// Javascript EMail Validation: RFC822
// Convertion; Change all (') for (") (EXCLUDE PARENTHESIS)
// Validate User E-Mail Textfield Input (Returns True if Valid, False if Invalid)
// Validate using the "RFC822" E-Mail rules
//
// Validate User E-Mail Textfield Input (Returns True if Valid, False if Invalid)
// Validate using the "RFC822" E-Mail rules
//function isValidEmail(TextFieldID, TextFieldIconID, TextFieldName) {
// Get the E-Mail
// var sEmail = TextFieldID.value;

// var sQtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
// var sDtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
// var sAtom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
// var sQuotedPair = '\\x5c[\\x00-\\x7f]';
// var sDomainLiteral = '\\x5b(' + sDtext + '|' + sQuotedPair + ')*\\x5d';
// var sQuotedString = '\\x22(' + sQtext + '|' + sQuotedPair + ')*\\x22';
// var sDomain_ref = sAtom;
// var sSubDomain = '(' + sDomain_ref + '|' + sDomainLiteral + ')';
// var sWord = '(' + sAtom + '|' + sQuotedString + ')';
// var sDomain = sSubDomain + '(\\x2e' + sSubDomain + ')*';
// var sLocalPart = sWord + '(\\x2e' + sWord + ')*';
// var sAddrSpec = sLocalPart + '\\x40' + sDomain; // complete RFC822 email address spec
// var sValidEmail = '^' + sAddrSpec + '$'; // as whole string
//
// var reValidEmail = new RegExp(sValidEmail);
// if (reValidEmail.test(sEmail)) {
// Return Valid
// return true;
// }else{
// Return Invalid
// return false;
// }
//   
// }
	
	  String sQtext = "[^\\x0d\\x22\\x5c\\x80-\\xff]";
	  String sDtext = "[^\\x0d\\x5b-\\x5d\\x80-\\xff]";
	  String sAtom = "[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+";
	  String sQuotedPair = "\\x5c[\\x00-\\x7f]";
	  String sDomainLiteral = "\\x5b(" + sDtext + "|" + sQuotedPair + ")*\\x5d";
	  String sQuotedString = "\\x22(" + sQtext + "|" + sQuotedPair + ")*\\x22";
	  String sDomain_ref = sAtom;
	  String sSubDomain = "(" + sDomain_ref + "|" + sDomainLiteral + ")";
	  String sWord = "(" + sAtom + "|" + sQuotedString + ")";
	  String sDomain = sSubDomain + "(\\x2e" + sSubDomain + ")*";
	  String sLocalPart = sWord + "(\\x2e" + sWord + ")*";
	  String sAddrSpec = sLocalPart + "\\x40" + sDomain; // complete RFC822 email address spec
	  String sValidEmail = "^" + sAddrSpec + "$"; // as whole string
	  
	  final String EMAIL_PATTERN = sValidEmail;
	  
	Pattern EMailPattern = Pattern.compile(EMAIL_PATTERN);
	Matcher EMailMatcher = EMailPattern.matcher(EMail);
	boolean EMailMatches =  EMailMatcher.matches();

	if(EMailMatches){
		System.out.println(EMail + " has a valid E-Mail Format.");
	}else{
		System.out.println("INTERNAL ERROR: isValidEmailAddress() ");
		System.out.println(EMail + " has an Invalid E-Mail Format.");
		throw new RuntimeException("Invalid E-Mail Format. Please provide a valid E-Mail.");
	}

	}

/*
private void isValidEmailAddress2(String EMail) {
	
// This Java E-Mail Validation Regex was generated by converting the JavaScript E-Mail Validation Regex
// Javascript EMail Validation: RFC822
// var Regex = (/^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/);
// Convertion; Change all (/) for (")   AND make all (\) into (\\) (EXCLUDE PARENTHESIS)
//
// function isValidEmail2(TextFieldID, TextFieldIconID, TextFieldName) {
// Javascript EMail Validation: RFC822
// var Regex = (/^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/);
// Get the E-Mail
// var EMail = TextFieldID.value;
// if (Regex.test(EMail)) {
// Return Valid
// return true;
// }else{
	// Return Invalid
// return false;
// }
// }
	
final String EMAIL_PATTERN = "^([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x22)(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x22))*\\x40([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x5d)(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x5d))*$";

Pattern EMailPattern = Pattern.compile(EMAIL_PATTERN);
Matcher EMailMatcher = EMailPattern.matcher(EMail);
boolean EMailMatches =  EMailMatcher.matches();

if(EMailMatches){
	System.out.println(EMail + " has a valid E-Mail Format.");
}else{
	System.out.println("INTERNAL ERROR: isValidEmailAddress() ");
	System.out.println(EMail + " has an Invalid E-Mail Format.");
	throw new RuntimeException("Invalid E-Mail Format.");
}

}
*/

/* 
// Really weak E-Mail validation implementation from import javax.mail.internet.InternetAddress;
private void isValidEmailAddress3(String EMail) {
	
	   try {
	      InternetAddress emailAddr = new InternetAddress(EMail);
	      // Validate that this address conforms to the syntax rules of RFC822.
	      // The current implementation checks many, but not all, syntax rules. 
	      // Note that even though the syntax of the address may be correct, there's no guarantee that a mailbox of that name exists. 
	      emailAddr.validate();
	      System.out.println(EMail + " has a valid E-Mail Format.");
	   } catch (AddressException EX) {
		   EX.printStackTrace();
		   System.out.println("INTERNAL ERROR: isValidEmailAddress() ");
		   System.out.println(EMail + " has an Invalid E-Mail Format.");
		   throw new RuntimeException("Invalid E-Mail Format.");

	   }
	   
	}
*/

}
